<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算平均值</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: #fff;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 500px;
            box-sizing: border-box;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            color: #666;
            margin-bottom: 10px;
            display: block;
        }

        input{
            width: 100%;
            padding: 12px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .message {
            margin-top: 15px;
            color: #e74c3c;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>计算平均值</h1>

        <label for="numbersInput">请输入数字（用逗号隔开）：</label>
        <input id="numbersInput" type="text" placeholder="例如：1, 2, 3, 4, 5" />

        <button onclick="calculateAverage()">计算平均值</button>

        <div id="result" class="result"></div>
        <div id="message" class="message"></div>
    </div>

    <script>
        const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;

        function calculateAverage() {
            const input = document.getElementById('numbersInput').value;
            const resultDiv = document.getElementById('result');
            const messageDiv = document.getElementById('message');

            // 处理用户输入的数字字符串
            const nums = input.split(',').map(num => num.trim()).filter(num => !isNaN(num) && num !== '').map(Number);

            if (nums.length > 0) {
                const avg = average(...nums);
                resultDiv.textContent = `平均值是：${avg}`;
                messageDiv.textContent = ''; // 清除错误消息
            } else {
                resultDiv.textContent = '';
                messageDiv.textContent = '请输入有效的数字，用逗号隔开。';
            }
        }
    </script>

</body>

</html>